package com.loto.insurance.mis.web.controller.ajax;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.servlet.ModelAndView;

import com.loto.insurance.mis.web.authority.INSAuthority;
import com.loto.insurance.mis.web.entity.ajax.BaseJson;
import com.loto.insurance.mis.web.entity.ajax.GridJson;
import com.loto.insurance.mis.web.entity.ajax.SupInfo;
import com.loto.insurance.mis.web.enumeration.CustTypeEnum;
import com.loto.insurance.mis.web.enumeration.IsActiveEnum;
import com.loto.insurance.mis.web.enumeration.MenuEnum;
import com.loto.insurance.mis.web.enumeration.ResultTypeEnum;
import com.loto.insurance.mis.web.service.SupInfoService;
import com.loto.insurance.mis.web.service.ProductInfoService;
import com.loto.insurance.mis.web.entity.Pager;
import com.loto.utils.DateUtils;

@Controller
@SessionAttributes("ins_user")
public class SupJsonController {

  @Autowired
  SupInfoService supInfoService;

  @Autowired
  ProductInfoService productInfoService;

  @INSAuthority(authorityMenu = MenuEnum.SUPPLIER_LIST, resultType = ResultTypeEnum.json)
  @RequestMapping(value = "/supplier/list.json", method = RequestMethod.POST)
  public ModelAndView supomerlist(
	  @RequestParam(value = "supName", required = false) String supName,
	  @RequestParam(value = "page", required = false, defaultValue = "1") int page,
	  @RequestParam(value = "rows", required = false, defaultValue = "20") int rows) {
	Map<String, Object> params = new HashMap<String, Object>();
	if (supName != null)
	  params.put("supName", supName);
	Pager pager = new Pager();
	pager.setPageSize(rows);

	params.put("pageSize", pager.getPageSize());
	params.put("startIndex", pager.getPageSize() * (page - 1));

	if (page > 1)
	  pager.setIndex(page);
	List<com.loto.insurance.mis.web.entity.SupInfo> supList = supInfoService
	    .findAllSup(params);

	List<SupInfo> list = new ArrayList<SupInfo>();
	for (com.loto.insurance.mis.web.entity.SupInfo sup : supList) {
	  SupInfo s = new SupInfo();
	  s.setSupId(sup.getSupId());
	  s.setSupName(sup.getSupName());
	  s.setSupCreatedOn(DateUtils.formatShortDateTime(sup.getSupCreatedOn()));

	  int supStatus = sup.getSupType();
	  for (IsActiveEnum e : IsActiveEnum.values()) {
		if (e.getIndex() == supStatus) {
		  s.setSupStatus(e.getName());
		  break;
		}
	  }
	  int supType = sup.getSupType();
	  for (CustTypeEnum e : CustTypeEnum.values()) {
		if (e.getIndex() == supType) {
		  s.setSupType(e.getName());
		  break;
		}
	  }

	  list.add(s);
	}

	pager.setRowCount(supInfoService.findAllSupCount(params));

	ModelAndView mav = new ModelAndView();

	GridJson<SupInfo> gridJson = new GridJson<SupInfo>();
	gridJson.setTotal(pager.getRowCount());
	gridJson.setRows(list);

	mav.addObject("result", gridJson);
	return mav;
  }

  @INSAuthority(authorityMenu = MenuEnum.SUPPLIER_LIST, resultType = ResultTypeEnum.json)
  @RequestMapping(value = "/supplier/add.json", method = RequestMethod.POST)
  public ModelAndView supomeradd(
	  @RequestParam(value = "name", required = true) String supName,
	  @RequestParam(value = "type", required = true) int supType,
	  @RequestParam(value = "status", required = true) int supStatus) {

	com.loto.insurance.mis.web.entity.SupInfo supInfo = new com.loto.insurance.mis.web.entity.SupInfo();

	supInfo.setSupName(supName);
	supInfo.setSupStatus(supStatus);
	supInfo.setSupType(supType);

	ModelAndView mav = new ModelAndView();
	BaseJson base = new BaseJson();
	base.setOk(supInfoService.addSup(supInfo));
	mav.addObject("result", base);
	return mav;
  }

  @INSAuthority(authorityMenu = MenuEnum.SUPPLIER_LIST, resultType = ResultTypeEnum.json)
  @RequestMapping(value = "/supplier/modify.json", method = RequestMethod.POST)
  public ModelAndView supomermodify(
	  @RequestParam(value = "id", required = true) long supId,
	  @RequestParam(value = "type", required = true) int supType,
	  @RequestParam(value = "status", required = true) int supStatus) {

	com.loto.insurance.mis.web.entity.SupInfo supInfo = new com.loto.insurance.mis.web.entity.SupInfo();
	supInfo.setSupId(supId);
	supInfo.setSupStatus(supStatus);
	supInfo.setSupType(supType);

	ModelAndView mav = new ModelAndView();
	BaseJson base = new BaseJson();
	base.setOk(supInfoService.updateSup(supInfo));
	mav.addObject("result", base);
	return mav;
  }
}
